package wendyeq.iweb.blog.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import wendyeq.iweb.blog.dao.ICommentDAO;
import wendyeq.iweb.blog.pojo.Article;
import wendyeq.iweb.blog.pojo.Comment;
import wendyeq.iweb.blog.util.JdoManager;

@Repository("commentDAO")
public class CommentDAO implements ICommentDAO {
	private static final Logger logger = Logger.getLogger(CommentDAO.class);

	@Override
	public boolean saveOrUpdate(Comment entity) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			//Article article = manager.getObjectById(Article.class, entity.getArticle().getId());
			//entity.setArticle(article);
			//article.getComments().add(comment); // important
			//logger.warn("artCom"+article.getComments().size());
			manager.makePersistent(entity);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally{
			manager.close();
		}
		return true;
	}

	@Override
	public boolean delete(String id) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Comment comment = manager.getObjectById(Comment.class, id);
			manager.deletePersistent(comment);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally {
			manager.close();
		}
		return true;
	}

	@Override
	public Comment findById(String id) {
		Comment comment = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			comment = manager.getObjectById(Comment.class, id);
		} catch (Exception e) {
			logger.error(e);
		}  
		return comment;
	}

	@Override
	public List<Comment> findByPage(int min, int max) {
		List<Comment> comments = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Comment.class);
			query.setOrdering("createdTime DESC");
			query.setRange(min, max);
			comments = (List<Comment>) query.execute();
			logger.warn("comments-size: "+comments.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return comments;
	}

	@Override
	public List<Comment> findAll() {
		List<Comment> comments = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Comment.class);
			query.setOrdering("createdTime DESC");
			comments = (List<Comment>) query.execute();
		} catch (Exception e) {
			logger.error(e);
		}
		return comments;
	}

	/*public Comment insert(Comment comment) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Article article = manager.getObjectById(Article.class, comment
					.getArticle().getId());
			comment.setArticle(article);
			//article.getComments().add(comment); // important
			//logger.warn("artCom"+article.getComments().size());
			manager.makePersistent(comment);
		} catch (Exception e) {
			logger.error(e);
		} 
		return comment;
	}
	
	public void update(Comment comment) {
		PersistenceManager manager = JdoManager.getSession();
		try{
			manager.makePersistent(comment);
		} catch (Exception e){
			logger.error(e);
		} finally{
			manager.close();
		}
	}
	
	public void delete(String id){
		PersistenceManager manager = JdoManager.getSession();
		try {
			Comment comment = manager.getObjectById(Comment.class, id);
			manager.deletePersistent(comment);
		} catch (Exception e) {
			logger.error(e);
		} finally {
			manager.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Comment> findAll() {
		List<Comment> comments = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Comment.class);
			query.setOrdering("createdTime DESC");
			comments = (List<Comment>) query.execute();
		} catch (Exception e) {
			logger.error(e);
		}
		return comments;
	}

	@SuppressWarnings("unchecked")
	public List<Comment> findByPage(int min, int max) {
		List<Comment> comments = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Comment.class);
			query.setOrdering("createdTime DESC");
			query.setRange(min, max);
			comments = (List<Comment>) query.execute();
			logger.warn("comments-size: "+comments.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return comments;
	}
	
	public Comment findById(String id) {
		Comment comment = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			comment = manager.getObjectById(Comment.class, id);
		} catch (Exception e) {
			logger.error(e);
		}  
		return comment;
	}*/

}
